<template>
  <div>
    <el-form ref="editFormRef" :rules="rules" :model="editForm.data" label-width="120">
      <el-row>
        <el-form-item label="车牌号码" prop="licensePlate">
          <el-input
            v-model="editForm.data.licensePlate"
            style="width: 320px"
            maxlength="11"
            placeholder="请输入车牌号码"
          />
        </el-form-item>
        <el-form-item label="车主手机" prop="ownerPhone">
          <el-input
            v-model="editForm.data.ownerPhone"
            style="width: 320px"
            placeholder="请输入车主手机"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item
          label="适用场库"
          prop="lotList"
          v-if="editForm.data.scope == 3 || editForm.data.scope == 1"
        >
          <el-cascader
            v-model="lotId"
            :options="lotIdList.data"
            style="width: 320px"
            placeholder="请选择适用场库"
            :props="{
              value: 'id',
              label: 'name',
              multiple: true,
            }"
            @change="handleLotIdChange"
            clearable
          />
        </el-form-item>
        <el-form-item
          label="适用路段"
          prop="sectionList"
          v-if="editForm.data.scope == 3 || editForm.data.scope == 2"
        >
          <el-cascader
            v-model="section"
            :options="sectionIdList.data"
            style="width: 320px"
            placeholder="请选择适用路段"
            :props="{
              value: 'id',
              label: 'name',
              multiple: true,
            }"
            @change="handleSectionChange"
            clearable
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="备注">
          <el-input
            v-model="editForm.data.remark"
            maxlength="20"
            show-word-limit
            type="textarea"
            style="width: 320px"
          />
        </el-form-item>
      </el-row>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(editFormRef)">取 消</el-button>
            <el-button class="confirm-btn" @click="submitForm(editFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { getEditCparkCard } from "../request.js";
const emits = defineEmits(["operationDone"]);

const props = defineProps({
  listData: {
    type: Object,
    default: () => {},
  },
  operationId: {
    type: Object,
    default: () => {},
  },
});
console.log(props.operationId);

const lotId = ref([]);
const lotIdList = reactive({
  data: [],
});
const section = ref([]);
const sectionIdList = reactive({
  data: [],
});

const editFormRef = ref("");
const editForm = reactive({
  data: {
    ownerPhone: props.operationId.ownerPhone,
    licensePlate: props.operationId.licensePlate,
    remark: props.operationId.remark,
    scope: props.operationId.scope,
  },
});

const handleLotIdChange = (value) => {
  console.log("value ===>", value);
  lotId.value = lotId.value
    .filter((item) => Array.isArray(item)) // 确保是数组
    .map((item) => item[0]);
  console.log("value ===>", lotId.value);

  editForm.data.lotList = lotIdList.data
    .filter((item) => lotId.value.includes(item.id))
    .map((item) => ({ id: item.id, name: item.name }));
};

const handleSectionChange = (value) => {
  console.log("value ===>", value);
  section.value = section.value
    .filter((item) => Array.isArray(item)) // 确保是数组
    .map((item) => item[0]);
  console.log("value ===>", section.value);

  editForm.data.sectionList = sectionIdList.data
    .filter((item) => section.value.includes(item.id))
    .map((item) => ({ id: item.id, name: item.name }));
};

const rules = reactive({
  licensePlate: [{ required: true, message: "请输入车牌号码", trigger: "blur" }],
  ownerPhone: [
    { required: true, message: "请输入车主手机", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "手机号码格式不正确，请重新输入",
      trigger: "blur",
    },
  ],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      const data = editForm.data;
      const id = props.operationId.id;
      getEditCparkCard({ data, id }).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  editForm.data = {};
  formEl.resetFields();
  emits("operationDone");
};

onMounted(() => {
  lotIdList.data = props.listData.lotIdList;
  sectionIdList.data = props.listData.sectionIdList;
  console.log(props.operationId.lotIds);

  if (props.operationId.lotIds) {
    lotId.value = props.operationId.lotIds.map((lot) => lot);

    editForm.data.lotList = lotIdList.data
      .filter((item) => lotId.value.includes(item.id))
      .map((item) => ({ id: item.id, name: item.name }));
  }
  if (props.operationId.secIds) {
    section.value = props.operationId.secIds.map((lot) => lot);

    editForm.data.secIds = sectionIdList.data
      .filter((item) => section.value.includes(item.id))
      .map((item) => ({ id: item.id, name: item.name }));
  }
});
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
